package com.supervision.manage.common;

import com.fly.base.misc.upload.FileSystemClient;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Util
 * @description:
 * @author: whis
 * @create: 2019-12-26 15:43
 **/
public class Util extends com.fly.base.util.Util {

    public static List<String> formatSortStr(String sortStr, String[] keys) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isEmpty(sortStr)) {
            return list;
        }
        sortStr = sortStr.replaceAll("ending", "");
        String[] sortArr = StringUtils.split(sortStr, "-");
        if (sortArr.length == 2) {
            if (Lists.newArrayList(keys).contains(sortArr[0])) {
                list.add(sortArr[0]);
                list.add(sortArr[1]);
            }
        }
        return list;
    }

    public static String orderByStr(String sortStr, String[] keys, String defaultOrderBy) {
        List<String> sortArr = Util.formatSortStr(sortStr, keys);
        if (sortArr.size() == 2) {
            return " order by " + sortArr.get(0) + " " + sortArr.get(1) + " ";
        }
        return defaultOrderBy;
    }

    public static String getImgUrl(String img) {
        if (StringUtils.isEmpty(img)) {
            return "";
        }
        return FileSystemClient.getClient().getDownloadUrl(img);
    }

}
